home *** CD-ROM | disk | FTP | other *** search
- // --------------------------------------------------------------------------------------
- //
- // Test.c
- // By Ben Manuto
- //
- // A Simple application to receive and send a SInt16 message via the Message system API.
- // Must be built with large model.
- //
- // c.1996, by Apple Computer, Inc. All rights reserved.
- //
- // --------------------------------------------------------------------------------------
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <conio.h>
- #include "PCMsgTst.h"
-
- #define kDefaultCmdType 0x6D736774 // 'msgt' - Our msg command OSType
- #define kDefaultMsgSize 256 // Our message packet will be 256 bytes.
- #define kDefaultMsgTypes 2 // We will register 2 cmd types for 'msgt'
-
- SInt16 gCompCalled;
- SInt16 gAckReceived;
-
-
- // ----------------------------------------------------------------------------------------
-
- SInt32 WaitMsg(MsgPBlkPtr theMsg)
- {
- SInt32 i=0;
-
- while ((theMsg->msgResult == 1) && (!(kbhit())));
-
- if (theMsg->msgResult) { // Did we get a recieve error?
- printf("\n\nWaitMsg():Rcv Error = %i\n", theMsg->msgResult);
- return theMsg->msgResult;
- }
-
- return theMsg->msgParam1;
- }
-
-
- // ----------------------------------------------------------------------------------------
-
- void __loadds RcvCompletion(void)
- {
- gCompCalled = true;
- }
-
-
- // ----------------------------------------------------------------------------------------
-
- void __loadds SendCompletion(void)
- {
- gCompCalled = true;
- }
-
-
- // ----------------------------------------------------------------------------------------
-
- void __loadds InitMsg(MsgPBlkPtr theMsg, SInt32 bytes, SInt32 completionRtn)
- {
- theMsg->msgFlags = 0;
- theMsg->msgReqCount = bytes;
- theMsg->msgActCount = 0;
- theMsg->msgCompletion = completionRtn;
- theMsg->msgResult = 1;
-
- gCompCalled = false;
- }
-
-
- // ----------------------------------------------------------------------------------------
-
- SInt16 SendAck(SInt16 cmdBase, SInt32 bytesReceived)
- {
- MsgPBlkPtr ackMessage;
- SInt16 key;
-
- ackMessage = _fmalloc(sizeof(MsgPBlk)); // Allocate space for the ackMessage PBlk.
-
- ackMessage->msgCmd = cmdBase+1; // Set the message type as an ack.
- ackMessage->msgParam1 = bytesReceived; // param1 is the bytes received.
- ackMessage->msgParam2 = 0L;
- ackMessage->msgBuffer = 0L;
-
- InitMsg(ackMessage, 0, (SInt32) SendCompletion); // Init the common variable and set the completion rtn.
- MsgSend(ackMessage); // Send the ackMessage
-
- key = 0;
- printf("Ack Sent......");
- while ((gCompCalled == false) && (key ==0)) { // Wait for the receive completion routine to be called.
- key = kbhit();
- }
-
- _ffree(ackMessage);
-
- if (key != 0) {
- printf("ack completion aborted!\n");
- return(-1);
- }
- else {
- printf("ack completion called.\n");
- return(0);
- }
-
- }
-
-
- // ----------------------------------------------------------------------------------------
-
- SInt16 WaitForAck(AckInfo *theAck)
- {
- SInt16 key, bytes = kDefaultMsgSize;
-
- key = 0;
- printf("Waiting for Ack......");
- while ((theAck->ackReceived == false) && (key ==0)) { // Wait for the receive completion routine to be called.
- key = kbhit();
- }
-
- if (key != 0) {
- printf("Wait aborted!\n");
- return(-1);
- }
- else {
- printf("Ack Received.\n");
-
- if (theAck->bytesReceived == (SInt32) bytes)
- printf("Message sent successfully. All %d bytes received by Mac.\n", bytes);
- else
- printf("The message was not received properly. %d bytes were sent and %d were received by the Mac.\n",
- bytes, theAck->bytesReceived);
-
- return(0);
- }
- }
-
-
- // ----------------------------------------------------------------------------------------
-
- void main()
- {
- MsgPBlkPtr aMessage;
- MsgRecElemPtr theMsgRec;
- AckInfo theAck;
- SInt16 err, cmdBase, i;
- SInt32 SInt32Err;
- SInt8 key;
-
- err = MsgAvailable(); // Is the messaging system available?
- printf("msgAvailable err: %04X\n",err);
-
- err = MsgRegister(kDefaultCmdType,
- kDefaultMsgTypes,
- &cmdBase); // Register our Command type.
- printf("MsgRegister err: %04X cmmd: %4X\n\n",
- err, cmdBase);
-
- theAck.bytesReceived = 0; // Init the ack info.
- theAck.ackReceived = false;
-
- theMsgRec = _fmalloc(sizeof(MsgRecElem)); // allocate space for the receiver block.
-
- theMsgRec->Link = 0x00; // Ptr to next link
- theMsgRec->Code = (SInt32) MsgHandler; // Ptr to the Code for this link
- theMsgRec->cmdBase = cmdBase; // the base message number for this proc
- theMsgRec->cmdCount = kDefaultMsgTypes; // the # of message numbers for this proc
- theMsgRec->userData = (SInt32) &theAck; // store a pointer to our ackInfo.
- theMsgRec->recVXD = 0x00;
-
-
- aMessage = _fmalloc(sizeof(MsgPBlk)); // Allocate space for the send block.
-
- aMessage->msgCmd = cmdBase; // Init up the MsgPBlk....
- aMessage->msgParam1 = 0x12345678; // just for fun...
- aMessage->msgParam2 = kDefaultMsgSize; // set param2 to buffer size (not required)
- aMessage->msgBuffer = _fmalloc(kDefaultMsgSize); // Allocate space for our buffer.
-
-
- if (aMessage->msgBuffer == 0L) { // Make sure our buffer got allocated.
- printf("\nBuffer was not allocated!!\n");
- exit(1);
- }
-
- MsgInstHandler(theMsgRec); // Install our message handler.
- printf("\nHandler installed\n");
-
- printf("Initializing message...\n");
- InitMsg(aMessage,
- kDefaultMsgSize,
- (SInt32) RcvCompletion); // Init our MsgPBlk.
-
- err = MsgReceive(aMessage); // Queue up our receiving MsgPBlk.
-
- printf("Waiting for Message....\n");
- SInt32Err = WaitMsg(aMessage); // Wait unitl MsgPBlk is no SInt32er busy.
-
- aMessage->msgBuffer -= kDefaultMsgSize; // Set back the buffer pointer.
-
- printf("\nRequested Bytes = %ld, Actual Bytes = %ld\n",
- aMessage->msgReqCount, aMessage->msgActCount);
- printf("Param1 = $%08lX = #%li Param2 = $%08lX = #%li\n",
- aMessage->msgParam1, aMessage->msgParam1,
- aMessage->msgParam2, aMessage->msgParam2);
-
- for (i = 0; i < kDefaultMsgSize; i++) { // Print the msgBuffer.
- printf("%2X ", (UInt8) ((aMessage->msgBuffer)[i]));
- if (((i+1) % 16) == 0) printf("\n");
- }
-
- printf("\nWaiting for Rcv completion routine to be called....\n");
-
- key = 0;
- while ((gCompCalled == false) && (key ==0)) { // Wait for the receive completion routine to be called.
- key = kbhit();
- }
- if (key != 0) goto Exit;
- printf("Rcv completion routine Called!\n\n");
-
- err = SendAck(cmdBase, aMessage->msgActCount); // Send an Ack to the Mac.
- if (err) goto Exit;
-
- for (i = 0; i < kDefaultMsgSize; i++) { // Reverse the data in the buffer.
- (aMessage->msgBuffer)[i] = (SInt8) (kDefaultMsgSize - i - 1);
- }
-
- InitMsg(aMessage,
- kDefaultMsgSize,
- (SInt32)SendCompletion); // Init a new MsgPBlk to send.
-
- gAckReceived = false; // Set the ackReceived flag false.
-
- MsgSend(aMessage); // Send the new PBlk.
-
- printf("Waiting for Send completion routine to be called....\n");
-
- key = 0;
- while ((gCompCalled == false) && (key ==0)) { // Wait for the completion routine to be called.
- key = kbhit();
- }
- if (key != 0) goto Exit;
- printf("Send completion routine Called!\n");
-
- err = WaitForAck(&theAck); // Wait for response from Mac.
-
- Exit:
- err = MsgRmvHandler(theMsgRec); // Remove the message handler
- printf("\nHandler removed. Err = %d\n", err);
-
- _ffree(aMessage->msgBuffer); // Free up the buffer space.
- _ffree(aMessage);
- _ffree(theMsgRec);
- }
-
-
-